<?php
/**
 * 订单汇总表模型
 *
 * @author   xionggan
 * @date     2019-09-17
 */

namespace app\common\model;

use think\Db;
use think\Model;

class CoreOrder extends Model
{
    protected $name  = 'core_order';
    protected $error = null;
    // 追加属性
    protected $append = [
        'status_text',
        'type_text',
        'outbound_text',
    ];

    const STATUS_0  = 0;
    const STATUS_1  = 1;
    const STATUS_2  = 2;
    const STATUS_3  = 3;
    const STATUS_4  = 4;
    const STATUS_5  = 5;
    const STATUS_6  = 6;
    const STATUS_7  = 7;
    const STATUS_8  = 8;
    const STATUS_9  = 9;
    const STATUS_10 = 10;
    const STATUS_11 = 11;

    const SERVICE_STATUS_0 = 0;
    const SERVICE_STATUS_1 = 1;
    const SERVICE_STATUS_2 = 2;

    const DELIVERY_STATUS_0 = 0;
    const DELIVERY_STATUS_1 = 1;
    const DELIVERY_STATUS_2 = 2;
    const DELIVERY_STATUS_3 = 3;

    const VERIFY_STATUS_0 = 0;
    const VERIFY_STATUS_1 = 1;
    const VERIFY_STATUS_2 = 2;

    const SELF_0 = 0;
    const SELF_1 = 1;

    const COMMENT_0 = 0;
    const COMMENT_1 = 1;

    const OUTBOUND_0 = 0;
    const OUTBOUND_1 = 1;

    const DISPLAY_0 = 0;
    const DISPLAY_1 = 1;

    const DELETE_0 = 0;
    const DELETE_1 = 1;

    const ORDERY_TYPE_1 = 6;
    const ORDERY_TYPE_2 = 7;
    const ORDERY_TYPE_3 = 9;
    const ORDERY_TYPE_4 = 4;
    const ORDERY_TYPE_5 = 5;
    const ORDERY_TYPE_6 = 1;
    const ORDERY_TYPE_7 = 2;
    const ORDERY_TYPE_8 = 3;
    const ORDERY_TYPE_9 = 8;

    const CANCEL_REASON_1 = 1;
    const CANCEL_REASON_2 = 2;
    const CANCEL_REASON_3 = 3;
    const CANCEL_REASON_4 = 4;
    const CANCEL_REASON_5 = 5;
    const RETURN_REASON_1 = 1;
    const RETURN_REASON_2 = 2;
    const RETURN_REASON_3 = 3;
    const RETURN_REASON_4 = 4;
    const RETURN_REASON_5 = 5;

    /**
     * 获取订单状态映射
     * @return  array   状态映射表
     */
    public function getStatusMap()
    {
        return [
            self::STATUS_0  => '待支付',
            self::STATUS_1  => '待发货',
            self::STATUS_2  => '待收货',
            self::STATUS_3  => '已收货',
            self::STATUS_4  => '申请退款',
            self::STATUS_5  => '售后',
            self::STATUS_6  => '完成',
            self::STATUS_7  => '已退款',
            self::STATUS_8  => '拒绝退款',
            self::STATUS_9  => '取消',
            self::STATUS_10 => '待处理',
            self::STATUS_11 => '作废',
        ];
    }

    /**
     * 获取订单售后状态映射
     * @return  array   状态映射表
     */
    public function getServiceStatusMap()
    {
        return [
            self::SERVICE_STATUS_0  => '无售后',
            self::SERVICE_STATUS_1  => '部分售后',
            self::SERVICE_STATUS_2  => '全部售后',
        ];
    }

    /**
     * 获取订单发货状态映射
     * @return  array   状态映射表
     */
    public function getDeliveryStatusMap()
    {
        return [
            self::DELIVERY_STATUS_0  => '待发货',
            self::DELIVERY_STATUS_1  => '部分发货',
            self::DELIVERY_STATUS_2  => '已发货',
            self::DELIVERY_STATUS_3  => '已收货',
        ];
    }

    /**
     * 获取订单核销状态映射
     * @return  array   状态映射表
     */
    public function getVerifyStatusMap()
    {
        return [
            self::VERIFY_STATUS_0  => '待核销',
            self::VERIFY_STATUS_1  => '部分核销',
            self::VERIFY_STATUS_2  => '已核销',
        ];
    }

    /**
     * 获取订单类型映射表
     * @return  array   类型映射表
     */
    public function getTypeMap()
    {
        return [
            self::ORDERY_TYPE_1 => ['name'  => '商家买单', 'prefix' => 'SC'],
            self::ORDERY_TYPE_2 => ['name'  => '商家订单', 'prefix' => 'SO'],
            self::ORDERY_TYPE_3 => ['name'  => '商家预约', 'prefix' => 'SR'],
            self::ORDERY_TYPE_4 => ['name'  => '社区订单', 'prefix' => 'SQ'],
            self::ORDERY_TYPE_5 => ['name'  => '装修订单', 'prefix' => 'ZX'],
            self::ORDERY_TYPE_6 => ['name'  => '商城购物', 'prefix' => 'GW'],
            self::ORDERY_TYPE_7 => ['name'  => '商城拼团', 'prefix' => 'PT'],
            self::ORDERY_TYPE_8 => ['name'  => '商城众筹', 'prefix' => 'ZC'],
            self::ORDERY_TYPE_9 => ['name'  => '楼盘认筹', 'prefix' => 'RC'],
        ];
    }

    /**
     * 获取订单出库状态映射表
     * @return  array   类型映射表
     */
    public function getOutboundMap()
    {
        return [
            self::OUTBOUND_0 => '否',
            self::OUTBOUND_1 => '是',
        ];
    }

    /**
     * 获取订单取消原因
     * @return  array   取消原因映射表
     */
    public function getCancelReasonMap()
    {
        return [
            self::CANCEL_REASON_1 => '不想买了',
            self::CANCEL_REASON_2 => '忘记选择优惠券',
            self::CANCEL_REASON_3 => '商品选多了/选少了/选漏了',
            self::CANCEL_REASON_4 => '地址选错了',
            self::CANCEL_REASON_5 => '其他',
        ];
    }

    /**
     * 获取订单退款原因
     * @return  array   取消原因映射表
     */
    public function getReturnReasonMap()
    {
        return [
            self::RETURN_REASON_1 => '商品损坏',
            self::RETURN_REASON_2 => '商品不符合描述',
            self::RETURN_REASON_3 => '信息填写错误',
            self::RETURN_REASON_4 => '多拍/拍错/不想要',
            self::RETURN_REASON_5 => '其他原因',
        ];
    }

    /**
     * 获取错误信息
     * @return  string   错误信息
     */
    public function getError()
    {
        return $this->error;
    }

    public function product()
    {
        return $this->hasMany('app\common\model\CoreOrderProduct', 'order_id', 'order_id');
    }

    public function pick()
    {
        return $this->hasOne('app\common\model\CoreOrderPick', 'order_id', 'order_id', 'left');
    }

    public function delivery()
    {
        return $this->hasMany('app\common\model\CoreOrderDelivery', 'order_id', 'order_id', 'left');
    }

    public function service()
    {
        return $this->hasMany('app\common\model\CoreOrderService','order_sn','order_sn');
    }

    public function getCreateTimeAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['create_time']);
    }

    public function getUpdateTimeAttr($value, $data)
    {
        return empty($data['update_time']) ? '' : date('Y-m-d H:i:s', $data['update_time']);
    }

    public function getServiceTimeAttr($value, $data)
    {
        return empty($data['service_time']) ? '' : date('Y-m-d H:i:s', $data['service_time']);
    }

    public function getStatusTextAttr($value, $data)
    {
        $map = $this->getStatusMap();

        if (!empty($data['status_str']) && $data['status'] != self::STATUS_5) {
            return $data['status_str'];
        } else {
            return $map[$data['status']] ?? '';
        }
    }

    public function getTypeTextAttr($value, $data)
    {
        $map = $this->getTypeMap();

        if (empty($data['type']) || empty($map[$data['type']])) {
            return '';
        }

        return $map[$data['type']]['name'];
    }

    public function getOutboundTextAttr($value, $data)
    {
        if (!isset($data['outbound'])) {
            return '';
        }
        return $data['outbound'] == 1 ? '是' : '否';
    }

}
